package org.teamh.connection.json.handler;

import java.util.LinkedList;

import org.teamh.connection.MessageReceiver;
import org.teamh.connection.json.NOOP;
import org.teamh.connection.json.command.JsonSender;
import org.teamh.connection.json.event.JsonEventObject;
import org.teamh.game.Start;
import org.teamh.game.StartGame;
import org.teamh.logic.datamodel.*;

public class JsonConnectionHandler implements MessageReceiver {

	private DatamodelManager manager;
	private LinkedList<JsonMasterHandler> handler;
//	private boolean inGame;
	private JsonSender jsonSender;
	private boolean handle;
	
	public JsonConnectionHandler(Start start, DatamodelManager manager) {
		this.handler = new LinkedList<JsonMasterHandler>();
		this.manager = manager;
		JsonSender.setStart(start);
//		this.inGame = false;
		
		handler.add(new GameHandler(start, manager));
		handler.add(new GuideHandler(start, manager));
		handler.add(new CreepMappingHandler(start, manager));
		handler.add(new CreepDescriptionHandler(start, manager));
		handler.add(new TowerMappingHandler(start, manager));
		handler.add(new TowerDescriptionHandler(start, manager));
		handler.add(new TileHandler(start, manager));
		handler.add(new CellHandler(start, manager));
		handler.add(new MapHandler(start, manager));
		handler.add(new TeamHandler(start, manager));
		handler.add(new UserHandler(start, manager));
		handler.add(new UserAssetsHandler(start, manager));
		handler.add(new WaypointHandler(start, manager));
		handler.add(new MessageHandler(start, manager));
		handler.add(new GarbageHandler(start, manager));
		handler.add(new TowerHandler(start, manager));
		handler.add(new CreepHandler(start, manager));
		handler.add(new ChatHandler(start, manager));
		handler.add(new FireOnHandler(start, manager));
		
		NOOP.start(start);
	}
	
	@Override
	public void receiveMessage(String line) {
		handle = false;
		JsonEventObject json = build(line);
		
		if (line != null) {
			for (JsonMasterHandler handler : this.handler) {
				handle = handler.handleMsg(json);
				if (handle) {
					break;
				}
			}
			if (!handle) {
				if (StartGame.PRINT_MESSAGES) {
					System.out.println("No json-handler found for " + line);
				}
			}
		} else {
			System.out.println("Connection lost");
			System.exit(0);
//			inGame = false;
//			start.getGui().setFollowingScreen(start.getGui().getLoginScreen());
//			start.getConnection().reconnect();
//			start.getGui().addEvent(new Event("timeout", ""));
		}
	}
	
	public JsonEventObject build(String line) {
		if(line == null) {
			return new JsonEventObject("", "garbage", "", "", "");
		}
		
		String[] str = line.split("\"");
		
		String ts = null;
		String source = null;
		String property = null;
		String oldValue = null;
		String newValue = null;
		
		if (line.startsWith("{")) {
			ts = str[3];
			source = str[7];
			property = str[11];
			if(15 < str.length) {
				if (str[13].equals("@ov")) {
					oldValue = str[15];
					if (17 < str.length) {
						if (str[17].equals("@nv")) {
							newValue = str[19];
						}
					}				
				} else {
					newValue = str[15];
				}
			}
			JsonEventObject jsonEvent = new JsonEventObject(ts, source, property, oldValue, newValue);
		
			return jsonEvent;
		}
		return new JsonEventObject("", "garbage", "", "", "");
	}

	public DatamodelManager getManager() {
		return manager;
	}

	public void setManager(DatamodelManager manager) {
		this.manager = manager;
	}
	
	public JsonSender getJsonSender() {
		return jsonSender;
	}
}
